{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import autogen\n",
    "\n",
    "# Load the configuration including the response format\n",
    "llm_config = autogen.LLMConfig.from_json(path=\"OAI_CONFIG_LIST\", cache_seed=42).where(model=[\"gpt-5-nano\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autogen import ConversableAgent\n",
    "\n",
    "my_agent = ConversableAgent(\n",
    "    name=\"helpful_agent\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"You are a poetic AI assistant, respond in rhyme.\",\n",
    ")\n",
    "\n",
    "chat_result = my_agent.run(message=\"In one sentence, what's the big deal about AI?\", max_turns=1)\n",
    "chat_result.process()\n",
    "\n",
    "print(chat_result.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Chat between two comedian agents\n",
    "\n",
    "# 1. Import our agent class\n",
    "from autogen import ConversableAgent\n",
    "\n",
    "# 2. Define our LLM configuration for OpenAI's GPT-4o mini,\n",
    "#    uses the OPENAI_API_KEY environment variable\n",
    "\n",
    "# 3. Create our agents who will tell each other jokes,\n",
    "#    with Jack ending the chat when Emma says FINISH\n",
    "jack = ConversableAgent(\n",
    "    \"Jack\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=(\"Your name is Jack and you are a comedian in a two-person comedy show.\"),\n",
    "    is_termination_msg=lambda x: \"FINISH\" in x[\"content\"],\n",
    ")\n",
    "emma = ConversableAgent(\n",
    "    \"Emma\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=(\n",
    "        \"Your name is Emma and you are a comedian \"\n",
    "        \"in a two-person comedy show. Say the word FINISH \"\n",
    "        \"ONLY AFTER you've heard 2 of Jack's jokes.\"\n",
    "    ),\n",
    ")\n",
    "\n",
    "# 4. Run the chat\n",
    "chat_result = jack.initiate_chat(\n",
    "    emma,\n",
    "    message=\"Emma, tell me a joke about goldfish and peanut butter.\",\n",
    ")\n",
    "\n",
    "# 5. Print the chat\n",
    "print(chat_result.chat_history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Group chat amongst agents to create a 4th grade lesson plan\n",
    "# Flow determined by Group Chat Manager automatically, and\n",
    "# should be Teacher > Planner > Reviewer > Teacher (repeats if necessary)\n",
    "\n",
    "# 1. Import our agent and group chat classes\n",
    "from autogen import ConversableAgent, GroupChat, GroupChatManager\n",
    "\n",
    "# Define our LLM configuration for OpenAI's GPT-4o mini\n",
    "# uses the OPENAI_API_KEY environment variable\n",
    "\n",
    "# Planner agent setup\n",
    "planner_message = \"Create lesson plans for 4th grade. Use format: <title>, <learning_objectives>, <script>\"\n",
    "planner = ConversableAgent(\n",
    "    name=\"planner_agent\", llm_config=llm_config, system_message=planner_message, description=\"Creates lesson plans\"\n",
    ")\n",
    "\n",
    "# Reviewer agent setup\n",
    "reviewer_message = \"Review lesson plans against 4th grade curriculum. Provide max 3 changes.\"\n",
    "reviewer = ConversableAgent(\n",
    "    name=\"reviewer_agent\", llm_config=llm_config, system_message=reviewer_message, description=\"Reviews lesson plans\"\n",
    ")\n",
    "\n",
    "# Teacher agent setup\n",
    "teacher_message = \"Choose topics and work with planner and reviewer. Say DONE! when finished.\"\n",
    "teacher = ConversableAgent(\n",
    "    name=\"teacher_agent\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=teacher_message,\n",
    ")\n",
    "\n",
    "# Setup group chat\n",
    "groupchat = GroupChat(agents=[teacher, planner, reviewer], speaker_selection_method=\"auto\", messages=[])\n",
    "\n",
    "# Create manager\n",
    "# At each turn, the manager will check if the message contains DONE! and end the chat if so\n",
    "# Otherwise, it will select the next appropriate agent using its LLM\n",
    "manager = GroupChatManager(\n",
    "    name=\"group_manager\",\n",
    "    groupchat=groupchat,\n",
    "    llm_config=llm_config,\n",
    "    is_termination_msg=lambda x: \"DONE!\" in (x.get(\"content\", \"\") or \"\").upper(),\n",
    ")\n",
    "\n",
    "# Start the conversation\n",
    "chat_result = teacher.initiate_chat(recipient=manager, message=\"Let's teach the kids about the solar system.\")\n",
    "\n",
    "# Print the chat\n",
    "print(chat_result.chat_history)"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Agent Quickstart Examples",
   "tags": [
    "agents",
    "tools",
    "quickstart",
    "examples",
    "autogen"
   ]
  },
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
